﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

/// <summary>
/// 切换图像数据库，需要先移除而不是取消勾选 AR Session Origin 的 AR Tracked Image Manager(Script) 脚本组件，然后添加该脚本到 AR Session Origin 上
/// </summary>
public class ChangeImageLib : MonoBehaviour
{
    /// <summary>
    /// mDefaultSelectedLibraryIndex 默认图像数据库对应的 Prefab
    /// </summary>
    [SerializeField]
    private GameObject mPrefabOnTrack;

    [SerializeField]
    XRReferenceImageLibrary[] mReferenceImageLibrary;

    private int mDefaultSelectedLibraryIndex = 0;
    private int mCurrentSelectedLibraryIndex = 0;
    private ARTrackedImageManager mTrackImageManager;

    private void Awake()
    {
        try
        {
            //mPrefabOnTrack = (GameObject)Resources.Load("Prefabs/Astronaut");
            //mReferenceImageLibrary = new XRReferenceImageLibrary[] {
            //    (XRReferenceImageLibrary)Resources.Load("Databases/RefImageLib1") ,
            //    (XRReferenceImageLibrary)Resources.Load("Databases/RefImageLib2")
            //};

            //LogUtils.warn("mReferenceImageLibrary[mDefaultSelectedLibraryIndex] = " + mReferenceImageLibrary[mDefaultSelectedLibraryIndex]);
            //LogUtils.debug("mReferenceImageLibrary[mDefaultSelectedLibraryIndex] =" + mReferenceImageLibrary[mDefaultSelectedLibraryIndex]);

            //mTrackImageManager = gameObject.AddComponent<ARTrackedImageManager>();
            //LogUtils.debug("ChangeImageLib mTrackImageManager=" + mTrackImageManager);
            //mTrackImageManager.referenceLibrary = mTrackImageManager.CreateRuntimeLibrary(mReferenceImageLibrary[mDefaultSelectedLibraryIndex]);        //mTrackImageManager.CreateRuntimeLibrary(mReferenceImageLibrary[0]);
            //mTrackImageManager.maxNumberOfMovingImages = 3;
            //mTrackImageManager.trackedImagePrefab = mPrefabOnTrack;
            //mTrackImageManager.enabled = true;
            //LogUtils.debugGreen("ChangeImageLib Awake Success");
        }
        catch (Exception e)
        {
            ToastUtil.showToast("ChangeImageLib Awake Exception " + e.Message);
            //如果在 PC 上直接运行，会提示: No image tracking subsystem found. This usually means image tracking is not supported.
            //如果 AR Session Origin 已经添加了 ARTrackedImageManager，则会提示: Object reference not set to an instance of an object，那个 mTrackImageManager 是空的
        }
    }

    // Start is called before the first frame update
    void Start()
    {
 
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void switchImageLib()
    {
        ToastUtil.showToast("ChangeImageLib switchImageLib");
        try
        {
            mCurrentSelectedLibraryIndex += 1;
            if (mCurrentSelectedLibraryIndex >= mReferenceImageLibrary.Length)
            {
                mCurrentSelectedLibraryIndex = 0;
            }
            SetReferenceImageLibrary(mCurrentSelectedLibraryIndex);
        }
        catch (Exception e)
        {
            ToastUtil.showToast(e.Message);
        }
    }

    private void SetReferenceImageLibrary(int selectedLibraryIndex = 0)
    {
        //mTrackImageManager.referenceLibrary = mTrackImageManager.CreateRuntimeLibrary(mReferenceImageLibrary[selectedLibraryIndex]);
        //mCurrentSelectedLibraryIndex = selectedLibraryIndex;
        //ToastUtil.showToast("切换参考图像数据库 {0} 成功" + selectedLibraryIndex);
    }
}
